package com.woniuxy.provider;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;

import com.woniuxy.pojo.Student;

public class StudentProvider {
	public String select(Map<String, Object> map) {
		Student student = (Student) map.get("student");
		int pageNumber = (int) map.get("pageNumber");
		int pageSize = (int) map.get("pageSize");
		String sql = new SQL() {
			{
				SELECT("*");
				FROM("student");
				if (student.getStudent_name() != null) {
					WHERE("student_name = #{student_name}");
				}
				if (student.getGrade_id() != 0) {
					WHERE("grade_id = #{grade_id}");
				}
				if (student.getHouse_id() != 0) {
					WHERE("house_id = #{house_id}");
				}
				// 从这个toString可以看出，其内部使用高效的StringBuilder实现SQL拼接
			}
		}.toString();
		if (pageSize != 0) {
			sql += " LIMIT " + pageNumber + "," + pageSize;
		}

		return sql;

	}

	// 符合条件的学生信息
	public String selectCount(Student student) {
		String sql = new SQL() {
			{
				SELECT("count(*)");
				FROM("student");
				if (student.getStudent_name() != null) {
					WHERE("student_name = #{student_name}");
				}
				if (student.getGrade_id() != 0) {
					WHERE("grade_id = #{grade_id}");
				}
				if (student.getHouse_id() != 0) {
					WHERE("house_id = #{house_id}");
				}
				// 从这个toString可以看出，其内部使用高效的StringBuilder实现SQL拼接
			}
		}.toString();

		return sql;

	}
}
